<template>
  <div>
    <el-row type=flex>
      <el-col :span="16">
        <el-button @click="handleCreate" icon="el-icon-plus" type="primary">新建进店有礼活动</el-button>
      </el-col>
      <el-col :span="8">
        <el-input placeholder="请输入进店有礼活动名称" v-model="filterForm.title">
          <el-button @click="handleFilter" slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <el-table :data="event.list" stripe class="hui-mt-1">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form :model="scope.row" label-width="8em" label-position="left" inline class="hui-data-table-expand">
            <el-form-item label="活动标题:">{{ scope.row.title }}</el-form-item>
            <el-form-item label="开始时间:">{{ $moment.unix(scope.row.startTime).format("YYYY-MM-DD") }}</el-form-item>
            <el-form-item label="结束时间:">{{ $moment.unix(scope.row.endTime).format("YYYY-MM-DD") }}</el-form-item>
            <el-form-item label="奖励类型:">{{ scope.row.type === 'coupon' ? '送优惠券' : scope.row.type === 'lucky' ? '送红包': scope.row.type }}</el-form-item>
            <el-form-item label="奖励:">
              <el-table :data="scope.row.reward" stripe border size="small" style="width: 320px; line-height: 23px;">
                <el-table-column label="标题" prop="title"></el-table-column>
                <el-table-column label="数量" prop="number"></el-table-column>
              </el-table>
            </el-form-item>
            <el-form-item label="参加用户限制:">{{ scope.row.eventAllUser ? '所有用户都可参加' : '部分用户可参加' }}</el-form-item>
            <el-form-item v-if="!scope.row.eventAllUser" label="可参加用户:">
              <el-tag v-for="item in scope.row.canEventUser" :key="item.cardId" size="small" type="primary" class="hui-mr-8px">{{ item.cardName }}</el-tag>
            </el-form-item>
            <el-form-item label="活动门店:">{{ scope.row.eventAllBranch ? '所有门店' : '部分门店' }}</el-form-item>
            <el-form-item v-if="!scope.row.eventAllBranch" label="活动门店列表:">
              <el-tag v-for="item in scope.row.canEventBranch" :key="item.branchId" size="small" type="primary" class="hui-mr-8px">{{ item.branchName }}</el-tag>
            </el-form-item>
            <el-form-item label="用户打标签:">
              <el-tag v-for="item in scope.row.userTags" :key="item.id" size="small" class="hui-mr-8px"># {{ item.label }} #</el-tag>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="活动标题 / 门店 / 状态">
        <template slot-scope="scope">
          <div>{{ scope.row.title }}</div>
          <div>{{ scope.row.eventAllBranch ? '所有门店' : '适用&nbsp;' + scope.row.canEventBranch.length + '&nbsp;门店' }}</div>
          <div>
            <el-tag v-if="scope.row.status === 0" size="small">未开始</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="success" size="small">进行中</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="info" size="small">已结束</el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="danger" size="small">已下线</el-tag>
            <el-tag v-else type="danger" size="small">{{ scope.row.status }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="活动时间">
        <template slot-scope="scope">
          <div>开始: {{ $moment.unix(scope.row.startTime).format("YYYY-MM-DD") }}</div>
          <div>结束: {{ $moment.unix(scope.row.endTime).format("YYYY-MM-DD") }}</div>
        </template>
      </el-table-column>
      <el-table-column label="最后操作人">
        <template slot-scope="scope">
          <div>{{ scope.row.updateUser }}</div>
          <div>{{ $moment.unix(scope.row.updateTime).format("YYYY-MM-DD HH:mm:ss") }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="primary" size="mini" :disabled="scope.row.status === 2 || scope.row.status === 3" plain>编辑</el-button>
          <el-button @click="handleDelete(scope.row)" :disabled="scope.row.status === 2 || scope.row.status === 3" type="danger" size="mini" plain>下线</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页，start-->
    <el-pagination @current-change="handleCurrentChange" :current-page.sync="event.pages" :page-sizes="[10]" :page-size="10" layout="total, sizes, prev, pager, next" :total="event.total" background class="hui-mt-2"></el-pagination>
    <!--分页，end-->
  </div>
</template>

<script>
export default {
  data () {
    return {
      filterForm: {
        title: ''
      },
      loading: false,
      event: {
        pages: 0,
        total: 0,
        list: []
      }
    }
  },
  created () {
    this.mockData()
  },
  methods: {
    // 筛选事件
    handleFilter () {
      if (this.filterForm.title === '') {
        return false
      } else {
        this.loading = true
        console.log(this.filterForm)
        this.$loading()
        setTimeout(() => {
          this.$loading().close()
          this.loading = false
        }, 500)
      }
    },
    mockData () {
      let pages = this.event.pages
      let total = 100
      let list = []
      let i = 10
      while (i > 0) {
        let _startTime = (val) => {
          return parseInt(this.$moment().day(val).set({'hour': 0, 'minute': 0, 'second': 0}).format('X'))
        }
        let _endTime = (val) => {
          return parseInt(this.$moment().day(val).set({'hour': 23, 'minute': 59, 'second': 59}).format('X'))
        }
        let _status = (min, max) => {
          min = Math.ceil(min)
          max = Math.floor(max)
          return Math.floor(Math.random() * (max - min + 1)) + min
        }
        let _updateTime = () => {
          return parseInt(this.$moment().format('X'))
        }
        list.push({
          id: i,
          title: '活动标题' + i,
          startTime: _startTime(1),
          endTime: _endTime(1 + i),
          status: _status(0, 3),
          updateTime: _updateTime(),
          updateUser: '黄倬, 13262268525',
          type: 'coupon',
          reward: [
            {
              cardId: '1',
              title: '优惠券标题1',
              number: 1
            },
            {
              cardId: '2',
              title: '优惠券标题2',
              number: 1
            }
          ],
          eventAllUser: i % 2 !== 0,
          canEventUser: i % 2 !== 0 ? [] : [{cardId: i, cardName: '会员卡名称' + i}, {cardId: i + 1, cardName: '会员卡名称' + (i + 1)}],
          eventAllBranch: i % 2 !== 0,
          canEventBranch: i % 2 !== 0 ? [] : [{branchId: i, branchName: '门店名称' + i}, {branchId: i + 1, branchName: '门店名称' + (i + 1)}],
          userTags: i % 2 !== 0 ? [] : [{id: i, label: '标签名称' + i}, {id: i + 1, label: '标签名称' + (i + 1)}]
        })
        i--
      }
      this.event.list = list
      this.event.pages = pages + 1
      this.event.total = total
    },
    handleCreate () {
      this.$router.push({
        path: 'detail',
        query: {
          type: 'create'
        }
      })
    },
    // 编辑事件
    handleEdit (row) {
      let id = row.id
      this.$router.push({
        path: 'detail',
        query: {
          type: 'edit',
          id: id
        }
      })
    },
    // 删除事件
    handleDelete (row) {
      this.$confirm('确认下线这个活动么？下线后，活动将立即结束。', '下线确认', {
        confirmButtonText: '下线',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('下线, 活动ID:' + row.id)
        this.$message({
          type: 'success',
          message: '活动已下线!'
        })
      }).catch(() => {})
    },
    // 分页事件
    handleCurrentChange () {
      console.log(this.event.pages)
      this.$loading()
      setTimeout(() => {
        this.$loading().close()
      }, 500)
    }
  }
}
</script>
